using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp1.Services;
using WpfApp1.ViewModels;
using WpfApp1.Models;

namespace WpfApp1.Views;

public partial class TeacherDashboard : Window
{
    private readonly TeacherDashboardViewModel _viewModel;
    public TeacherDashboard(string teacherNumber, 
        ITeacherCourseService teacherCourseService,
        ICourseSelectService courseSelectService)
    {
        InitializeComponent();
        _viewModel = new TeacherDashboardViewModel(teacherNumber, 
            teacherCourseService, courseSelectService);
        DataContext = _viewModel;
        
        // 加载教师课程
        _viewModel.LoadTeacherCourses();
        // 加载所有任课学生
        _viewModel.LoadAllTeacherStudents();
    }
    //点击查看选择我某门课的学生
    private void ViewStudents_Click(object sender, RoutedEventArgs e)
    {
        if (sender is Button button && button.Tag is string courseNumber)
        {
            _viewModel.LoadCourseStudents(courseNumber);
            // 切换到学生成绩管理标签页
            var tabControl = this.FindName("MainTabControl") as TabControl;
            if (tabControl != null)
            {
                tabControl.SelectedIndex = 1; // 切换到第二个标签页
            }
        }
    }
    private void GradeStudent_Click(object sender, RoutedEventArgs e)
    {
        if (sender is Button button && 
            button.Tag is StudentCourseSelection student &&
            button.Parent is StackPanel panel)
        {
            var gradeInput = panel.Children[0] as TextBox;
            if (gradeInput != null && int.TryParse(gradeInput.Text, out int grade))
            {
                _viewModel.GradeStudent(student.StudentNumber, student.CourseNumber, grade);
            }
            else
            {
                MessageBox.Show("请输入有效的成绩！");
            }
        }
    }
    private void SearchStudentByNumber_Click(object sender, RoutedEventArgs e)
    {
        _viewModel.SearchStudentByNumber(SearchStudentNumberTextBox.Text);
    }
    //显示全部
    private void ShowAllStudents_Click(object sender, RoutedEventArgs e)
    {
        _viewModel.ShowAllStudents();
    }

    //窗口移动
    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            this.DragMove();
        }
    }

    //关闭窗口
    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}
